package net.ssta.util.fusionchart;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class FusionCharts {
	public static final String SEPARATOR_CHAR ="ш"; 
	/**
	 * 分组 默认分组
	 */
	public static final String default_category = "defaultCategory";
	/**
	 * 链接类型：自定义
	 */
	public static final int linkType_custom = 0;
	/**
	 * 链接类型：内置AJAX
	 */
	public static final int linkType_ajax = 1;
	/**
	 * 链接类型 无连接
	 */
	public static final int linkType_noLink = 2;
	/**
	 * 显示数据
	 */
	public static final String showValue_Yes = "1";
	/**
	 * 不显示数据
	 */
	public static final String showValue_No= "0";
	
	public static String exportHttp = "http://192.168.1.105:8090/sdr/FCExporterContentPic.jsp";
	public static final String chartParam_exportEnabled = "exportEnabled";
	public static final String chartParam_exportFormats = "exportFormats";
	public static final String chartParam_exportFileName = "exportFileName";
	public static final String chartParam_exportAtClient = "exportAtClient";
	public static final String chartParam_exportAction = "exportAction";
	public static final String chartParam_exportHandler = "exportHandler";
	private String  PYAxisName = null;
	private String  SYAxisName = null;
//	private boolean PrimaryYAisName;
	
    private static String chartNoDataText = "没有查询到对应数据";
    private  List mssByNames = new ArrayList();
	private String yAxisMinValue = null;
	private String yAxisMaxValue = null;
	private boolean showValue;
	private boolean showName;
	private int width;
	private int height;
	private String plotFillRatio = null;
	private String title; 
	private String subTitle = null;
	private String showSum = "1";
	private String  numberPrefix = "";
	private String numberSuffix = "";
	private String baseFontSize = null;
	private String outCnvBaseFontSize = null;
	private String showLegend = null; 
	private List categorys = new ArrayList();
	private Map categorysMap = new HashMap();
	private List names = new ArrayList();
	private Map namesMap = new HashMap();
	private Map dataMap = new HashMap();
	private String xAxisName = null;
	private String yAxisName = null;
	private String realPath = null;
	private Map nameRendAsMap = new HashMap();
	private List fontList = new ArrayList();
	private Map simpleColorMap = new HashMap();
	private Map categoryColorMap = new HashMap();
	private Map categoryParentYAxisMap = new HashMap();
	private boolean isTransparent = false;
	private Map chartAttribute = new HashMap();
	private static Map<String,String> chartTypeMap = new LinkedHashMap<String,String>();
	
	private final String allowParam = "EXPORTENABLED,EXPORTFORMATS,EXPORTFILENAME,EXPORTATCLIENT,EXPORTACTION,EXPORTHANDLER,LABELDISPLAY,SLANTLABELS";
	
	
	public String getPYAxisName() {
		return PYAxisName;
	}

	public void setPYAxisName(String axisName) {
		PYAxisName = axisName;
	}

	public String getSYAxisName() {
		return SYAxisName;
	}

	public void setSYAxisName(String axisName) {
		SYAxisName = axisName;
	}

	
	/**
	 * 设置访问应用的URL地址，不以/结尾，例如http://localhost:8090/sdr
	 * @param url
	 */
	public static void setApplicationRootUrl(String url)
	{
		exportHttp = url + "/FCExporterContentPic.jsp";
	}
	
	public void setChartAttribute(String name,String param) 
	{
		if(allowParam.indexOf(name.toUpperCase()) > -1)
		{
			chartAttribute.put(name,param);
		}
		else
		{
			throw new RuntimeException("传入了不允许的参数,可以传入的参数为"+allowParam);
		} 
	}
	
	public Map getChartAttribute()
	{
		return chartAttribute;
	}
	/**
	 * 设置普通图中指定名称的线条颜色
	 * @param name     名称
	 * @param color     颜色 如FF0000
	 */
	public void setSimpleColor(String name,String color)
	{
		simpleColorMap.remove(name);
		simpleColorMap.put(name,color);
	}
	/**
	 * 设置分类图中指定名称的线条颜色
	 * @param name     名称
	 * @param color     颜色 如FF0000
	 */
	public void setCategoryColor(String name,String color)
	{
		categoryColorMap.remove(name);
		categoryColorMap.put(name,color);
	}
	
	/**
	 * 设置集合使用那个纵坐标轴
	 * @param name
	 * @param parentYAxis 取值为P或者S,如果为其他值，则默认为P
	 */
	public void setCategoryParentYAxis(String name,String parentYAxis)
	{
		categoryParentYAxisMap.put(name,"S".equals(parentYAxis) ? "S":"P");
	}
	
	public String getCategoryParentYAxis(String name)
	{
		String parentYAxis = (String)categoryParentYAxisMap.get(name);
		return parentYAxis == null ? "P" : parentYAxis;
	}
	
	public String getSimpleColor(String name)
	{
		return (String)simpleColorMap.get(name);
	}
	public String getCategoryColor(String name)
	{
		return (String)categoryColorMap.get(name);
	}
	/**
	 * 设置字体
	 * @param font 字体
	 * @param toObject 作用范围
	 */
	public void setFont(ChartsFont font,String toObject)
	{
		FontArea fa = new FontArea();
		fa.setFont(font);
		fa.setToObject(toObject);
		fontList.add(fa);
	}
	/**
	 * 设置副标题
	 * @param subtitle
	 */
	
	public void setSubTitle(String subtitle)
	{
		this.subTitle = subtitle;
	}
	public void addNameRendAs(String name,String rendAsType)
	{
		nameRendAsMap.put(name.trim(),rendAsType.trim());
	}
	public String getNameRendAs(String name)
	{
		Object o = nameRendAsMap.get(name.trim());
		if(o==null)
		{
			return null;
		}
		else
		{
			String s = (String)o;
			if("".equalsIgnoreCase(s.trim()))
			{
				return null;
			}
			else
			{
				return s.trim();
			}
		}
	}
	
	public FusionCharts(String title)
	{
		this(title,true,true);
	};
	public FusionCharts(String title,boolean showName,boolean showValue)
	{
		this.title = title;
		this.showName = showName;
		this.showValue = showValue;
		this.setChartAttribute(FusionCharts.chartParam_exportEnabled,"1");
		this.setChartAttribute(FusionCharts.chartParam_exportAtClient, "1");
		this.setChartAttribute(FusionCharts.chartParam_exportFormats,"JPEG=输出JPEG");
		this.setChartAttribute(FusionCharts.chartParam_exportHandler,exportHttp);
		/*
		 * 对图表字体进行设置
		 */
		ChartsFont chartsFont = new ChartsFont();
		chartsFont.setFontSize("12");
		ChartsFont chartsFontTitle = new ChartsFont();
		chartsFontTitle.setFontSize("14");
		this.setFont(chartsFontTitle, chartsFontTitle.toObject_Title);
		this.setFont(chartsFont, chartsFont.toObject_DataLabels);
		this.setFont(chartsFont, chartsFont.toObject_XAxisName);
		this.setFont(chartsFont, chartsFont.toObject_YAxisName);
		this.setFont(chartsFont, chartsFont.toObject_ToolTip);	
		this.outCnvBaseFontSize = "12";
		try 
		{
			this.setChartAttribute(FusionCharts.chartParam_exportFileName, java.net.URLEncoder.encode(title, "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
//	public static String getSwfPath(String swf)
//	{
//		StringBuffer buf = new StringBuffer();
//		buf.append(BaseConfig.getContextPath())
//		   .append(BaseConfig.getValue(BaseConfig.FUSIONCHARTS_PATH_KEY))
//		   .append("/")
//		   .append(swf)
//		   .append(".swf");
//		return buf.toString();
//	}
	/**
	 * 增加自定义链接数据
	 * @param category  分类
	 * @param name      名称
	 * @param value     值
	 * @param url       自定义URL
	 */
	public void addDataByCustom(String category,String name,double value,String url)
	{
	    this.addDataByCustom(category, name, value, url,null);
	}
	/**
	 * 增加自定义链接数据
	 * @param category  分类
	 * @param name      名称
	 * @param value     值
	 * @param url       自定义URL
	 * @param showValue 是否需要显示数据
	 */
	public void addDataByCustom(String category,String name,double value,String url,String showValue)
	{
		addDataByCustom(category, name, value, url, showValue, null);
	}
	
	/**
	 * 增加自定义链接数据
	 * @param category  分类
	 * @param name      名称
	 * @param value     值
	 * @param url       自定义URL
	 * @param showValue 是否需要显示数据
	 * @param toolText tooltip显示字段
	 */
	public void addDataByCustom(String category,String name,double value,String url,String showValue,String toolText)
	{
		ChartsData cd = new ChartsData();
		cd.setCategory(category);
		cd.setName(name);
		cd.setValue(value);
		cd.setUrl(url);
		cd.setLinkType(this.linkType_custom);
		cd.setShowValue(showValue);
		cd.setToolText(toolText);
		this.addData(cd);
	}
	
	public void addDataByCustom(String name,double value,String url)
	{
		addDataByCustom(this.default_category,name,value,url);
	}
	
	/**
	 * 增加AJAX链接数据
	 * @param category 分类
	 * @param name     名称
	 * @param value    值
	 * @param classname AJAX实现类
	 * @param params    参数
	 * @param swf       SWF
	 */
//	public void addDataByAjax(String category,String name,double value,String classname,String params,String swf)
//	{
//		addDataByAjax(category, name, value, classname, params, swf,null);
//	}
	
	/**
	 * 增加AJAX链接数据
	 * @param category 分类
	 * @param name     名称
	 * @param value    值
	 * @param classname AJAX实现类
	 * @param params    参数
	 * @param swf       SWF
	 * @param showValue 是否需要显示数据
	 */
//	public void addDataByAjax(String category,String name,double value,String classname,String params,String swf,String showValue)
//	{
//		addDataByAjax(category, name, value, classname, params, swf, showValue, null);
//	}
	
	/**
	 * 增加AJAX链接数据
	 * @param category 分类
	 * @param name     名称
	 * @param value    值
	 * @param classname AJAX实现类
	 * @param params    参数
	 * @param swf       SWF
	 * @param showValue 是否需要显示数据
	 * @param toolText tooltip显示字段
	 */
//	public void addDataByAjax(String category,String name,double value,String classname,String params,String swf,String showValue,String toolText)
//	{
//		ChartsData cd = new ChartsData();
//		cd.setCategory(category);
//		cd.setName(name);
//		cd.setValue(value);
//		cd.setClassname(classname);
//		cd.setParams(params);
//        cd.setSwf(getSwfPath(swf));
//        cd.setShowValue(showValue);
//		cd.setLinkType(this.linkType_ajax);
//		cd.setToolText(toolText);
//		this.addData(cd);
//	}
	
//	public void addDataByAjax(String name,double value,String classname,String params,String swf)
//	{
//		addDataByAjax(this.default_category,name,value,classname,params,swf);
//	}
	/**
	 * 增加无链接数据	
	 * @param category 分类
	 * @param name     名称
	 * @param value    值
	 */
	
	public void addDataByNoLink(String category,String name,double value)
	{
		addDataByNoLink(category, name, value,null);
	}

	/**
	 * 增加无链接数据	
	 * @param category 分类
	 * @param name     名称
	 * @param value    值
	 * @param showValue 是否需要显示数据
	 */
	
	public void addDataByNoLink(String category,String name,double value,String showValue)
	{
		addDataByNoLink(category, name, value, showValue, null);
	}
	
	
	/**
	 * 增加无链接数据	
	 * @param category 分类
	 * @param name     名称
	 * @param value    值
	 * @param showValue 是否需要显示数据
	 * @param toolText tooltip显示字段
	 */
	
	public void addDataByNoLink(String category,String name,double value,String showValue,String toolText)
	{
		ChartsData cd = new ChartsData();
		cd.setCategory(category);
		cd.setName(name);
		cd.setValue(value);
		cd.setLinkType(this.linkType_noLink);
		cd.setShowValue(showValue);
		cd.setToolText(toolText);
		this.addData(cd);
	}
	
	public void addDataByNoLink(String name,double value)
	{
		addDataByNoLink(this.default_category,name,value);
	}
	
	private void addData(ChartsData cd)
	{
		if(!categorys.contains(cd.getCategory()))
		{
			categorys.add(cd.getCategory());
		}
		if(!names.contains(cd.getName()))
		{
			names.add(cd.getName());
		}
		Object o = categorysMap.get(cd.getCategory());
		List temp = null;
		if(o==null)
		{
			temp = new ArrayList();
		}
		else
		{
			temp = (ArrayList)o;
			categorysMap.remove(cd.getCategory());
		}
		temp.add(cd);
		categorysMap.put(cd.getCategory(), temp);
		o = namesMap.get(cd.getName());
		temp = null;
		if(o==null)
		{
			temp = new ArrayList();
		}
		else
		{
			temp = (ArrayList)o;
			namesMap.remove(cd.getName());
		}
		temp.add(cd);
		namesMap.put(cd.getName(), temp);
		dataMap.put(cd.getCategory()+SEPARATOR_CHAR+cd.getName(), cd);
	}
	public List getDataByCategory(String category)
	{
          Object o = categorysMap.get(category);
          if(o==null)
          {
        	  return new ArrayList();
          }
          else
          {
        	  return (ArrayList)o;
          }
	}
	
	public ChartsData getDataByCategoryAndName(String category,String name)
	{
          Object o = dataMap.get(category+SEPARATOR_CHAR+name);
          if(o==null)
          {
        	  return null;
          }
          else
          {
        	  return (ChartsData)o;
          }
	}
	
	public List getDataByName(String name)
	{
          Object o = namesMap.get(name);
          if(o==null)
          {
        	  return new ArrayList();
          }
          else
          {
        	  return (ArrayList)o;
          }
	}
	
	
///**
// *  生成不分组HTML
// * @param swf  生成SWF  
// * @param categoryName  分类名
// * @param xmlFile 要生成的文件
// * @return
// */
//	public String getSimpleHtml(String categoryName,String swf,String xmlFile)
//	{
//		String rondom = UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
//		String chartDiv = "chartDiv" + rondom;
//		String chartId = "chartId" + rondom;
//	    XmlBuilder.writeXmlFile(realPath + "/" +xmlFile, "gb2312", XmlBuilder.getSimpleXML(this, categoryName, chartDiv, chartId));
//		return HtmlBuilder.getHtml(this, swf,chartDiv, chartId, BaseConfig.getContextPath() , xmlFile);
//	}
//	
//	public String getSimpleHtml(String swf,String xmlFile)
//	{
//		return getSimpleHtml(this.default_category,swf,xmlFile);
//	}
//	/**
//	 *  生成不分组XML 
//	 * @param categoryName  分类名
//	 * @param xmlFile 要生成的文件
//	 * @param chartDiv DIV名
//	 * @param chartId 图标ID
//	 * @return
//	 */
//	public String getSimpleXml(String categoryName,String xmlFile,String chartDiv,String chartId)
//	{
//		XmlBuilder.writeXmlFile(realPath + "/" +xmlFile, "gb2312", XmlBuilder.getSimpleXML(this, categoryName, chartDiv, chartId));
//		return BaseConfig.getContextPath() + xmlFile;
//	}
//	
//	public String getSimpleXml(String xmlFile,String chartDiv,String chartId)
//	{
//		return getSimpleXml(this.default_category,xmlFile,chartDiv,chartId);
//	}
//	/**
//	 * 生成分组XML
//	* @param xmlFile 要生成的文件
//	 * @param chartDiv DIV名
//	 * @param chartId 图标ID
//	 * @return
//	 */
//	public String getCategorysXml(String xmlFile,String chartDiv,String chartId)
//	{
//		XmlBuilder.writeXmlFile(realPath + "/" +xmlFile, "gb2312", XmlBuilder.getCategorysXML(this, chartDiv, chartId));
//		return BaseConfig.getContextPath() + xmlFile;
//	}
//	/**
//	 * 
//	@param swf  生成SWF  
//     * @param xmlFile 要生成的文件
//	 * @return
//	 */
//	public String getCategorysHtml(String swf,String xmlFile)
//	{
//		String rondom = UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
//		String chartDiv = "chartDiv" + rondom;
//		String chartId = "chartId" + rondom;
//		XmlBuilder.writeXmlFile(realPath + "/" +xmlFile, "gb2312", XmlBuilder.getCategorysXML(this, chartDiv, chartId));
//	    return HtmlBuilder.getHtml(this, swf,chartDiv, chartId, BaseConfig.getContextPath(), xmlFile);
//	}
	
	public void saveDataAsXml(String swf,File xmlFile)
	{
		if(chartTypeMap.get(swf).equals("2")) 
		XmlBuilder.writeXmlFile(xmlFile, "gb2312", XmlBuilder.getCategorysXML(this));
		else if(chartTypeMap.get(swf).equals("1"))
			XmlBuilder.writeXmlFile(xmlFile, "gb2312", XmlBuilder.getSetsXML(this));
		else if(chartTypeMap.get(swf).equals("3"))
			XmlBuilder.writeXmlFile(xmlFile, "gb2312", XmlBuilder.getCombineXML(this));
	}
	
//	/**
//	 * 生成分组堆积XML
//	* @param xmlFile 要生成的文件
//	 * @param chartDiv DIV名
//	 * @param chartId 图标ID
//	 * @return
//	 */
//	public String getMssByNamesXml(String xmlFile,String chartDiv,String chartId)
//	{
//		XmlBuilder.writeXmlFile(realPath + "/" +xmlFile, "gb2312", XmlBuilder.getMssByNamesXML(this, chartDiv, chartId));
//		return BaseConfig.getContextPath() + xmlFile;
//	}
//	/**
//	 * 
//	@param swf  生成SWF  
//     * @param xmlFile 要生成的文件
//	 * @return
//	 */
//	public String getMssByNamesHtml(String swf,String xmlFile)
//	{
//		String rondom = UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
//		String chartDiv = "chartDiv" + rondom;
//		String chartId = "chartId" + rondom;
//		XmlBuilder.writeXmlFile(realPath + "/" +xmlFile, "gb2312", XmlBuilder.getMssByNamesXML(this, chartDiv, chartId));
//	    return HtmlBuilder.getHtml(this, swf,chartDiv, chartId, BaseConfig.getContextPath(), xmlFile);
//	}
	
	public  void setShowValue(boolean showValue) {
		this.showValue = showValue;
	}
	public  void setShowName(boolean showName) {
		this.showName = showName;
	}
	public List getCategorys() {
		return categorys;
	}
	public void setCategorys(List categorys) {
		this.categorys = categorys;
	}
	public  boolean isShowValue() {
		return showValue;
	}
	public  boolean isShowName() {
		return showName;
	}
	public String getTitle() {
		return title;
	}
	/**
	 * 设置标题
	 * @param subtitle
	 */
	public void setTitle(String title) {
		this.title = title;
	}
	public int getWidth() {
		return width;
	}
	public int getHeight() {
		return height;
	}
	public List getNames() {
		return names;
	}
	public String getXAxisName() {
		return xAxisName;
	}
	public void setXAxisName(String axisName) {
		xAxisName = axisName;
	}
	public String getYAxisName() {
		return yAxisName;
	}
	public void setYAxisName(String axisName) {
		yAxisName = axisName;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public String getSubTitle() {
		return subTitle;
	}
	public List getFontList() {
		return fontList;
	}
	/**
	 * 是否显示总和
	 * @param showSum
	 */
	public void setShowSum(boolean showSum) {
		if(showSum)
		{
			this.showSum = "1";
		}
		else
		{
			this.showSum = "0";
		}
	}
	/**
	 * 设置数字前缀
	 * @param numberPrefix
	 */
	public void setNumberPrefix(String numberPrefix) {
		this.numberPrefix = numberPrefix;
	}
	/**
	 * 设置数字后缀
	 * @param numberSuffix
	 */
	public void setNumberSuffix(String numberSuffix) {
		this.numberSuffix = numberSuffix;
	}
	
	/**
	 * 设置基本字号
	 * @param numberPrefix
	 */
	public void setBaseFontSize(String baseFontSize) {
		this.baseFontSize = baseFontSize;
	}
	
	/**
	 * 设置外围字号
	 * @param numberPrefix
	 */
	public void setOutCnvBaseFontSize(String outCnvBaseFontSize) {
		this.outCnvBaseFontSize = outCnvBaseFontSize;
	}
	
	public String getShowSum() { 
		return showSum;
	}
	public String getNumberPrefix() {
		return numberPrefix;
	}
	public String getNumberSuffix() {
		return numberSuffix;
	}
	public String getBaseFontSize() {
		return baseFontSize;
	}
	public String getOutCnvBaseFontSize() {
		return outCnvBaseFontSize;
	}
	
	public String getPlotFillRatio() {
		return plotFillRatio;
	}
	public void setPlotFillRatio(String plotFillRatio) {
		this.plotFillRatio = plotFillRatio;
	}
	
	public boolean isTransparent() {
		return isTransparent;
	}
	public void setTransparent(boolean isTransparent) {
		this.isTransparent = isTransparent;
	}
	public String getYAxisMinValue() {
		return yAxisMinValue;
	}
	public void setYAxisMinValue(String axisMinValue) {
		yAxisMinValue = axisMinValue;
	}
	public String getYAxisMaxValue() {
		return yAxisMaxValue;
	}
	public void setYAxisMaxValue(String axisMaxValue) {
		yAxisMaxValue = axisMaxValue;
	}
	public static String getChartNoDataText() {
		return chartNoDataText;
	}
	public static void setChartNoDataText(String chartNoDataText) {
		FusionCharts.chartNoDataText = chartNoDataText;
	}
	public  List getMssByNames() {
		return mssByNames;
	}
	public  void setMssByNames(List mssByNames) {
		mssByNames = mssByNames;
	}
	public void addMssByNames(String[] names)
	{
		mssByNames.add(names);
	}
	public String getShowLegend() {
		return showLegend;
	}
	public void setShowLegend(boolean showLegend) {
		if(showLegend)
		{
			this.showLegend = "1";
		}
		else
		{
			this.showLegend = "0";
		}
	}
	
	static
	{
		chartTypeMap.put("Column3D","1");
		chartTypeMap.put("Pie3D","1");
		chartTypeMap.put("MSColumnLine3D","2");
		chartTypeMap.put("MSLine","2");
		chartTypeMap.put("MSColumn3D","2");
		chartTypeMap.put("MSColumn3DLineDY","3");
		
	}
}
